package com.mz.ozy.activity.me;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.MyGQAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.FriendBean;
import com.mz.ozy.bean.MyBounsBean;
import com.mz.ozy.bean.MyBounsRecodeBean;
import com.mz.ozy.bean.NewBonusBean;
import com.mz.ozy.bean.NewBonusDetailBean;
import com.mz.ozy.bean.NewBonusDonateBean;
import com.mz.ozy.bean.NewGQBean;
import com.mz.ozy.bean.PronotionBean;
import com.mz.ozy.mvp.presenter.MyBounsPresenter;
import com.mz.ozy.mvp.view.MyBonusContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GQActivity extends BaseActivity<MyBonusContract.View, MyBounsPresenter> implements MyBonusContract.View  {

    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private int tag = 0;
    private int pageNo = 1;
    private List<NewGQBean.PageDTO.PageItemsDTO> mBeanList = new ArrayList<>();
    private MyGQAdapter mAdapter;

    private String gq;

    public static void go(Activity activity, String gq) {
        Intent intent = new Intent(activity, GQActivity.class);
        intent.putExtra("gq", gq);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gq);

        gq = getIntent().getStringExtra("gq");
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("GQ");
        tvTotal.setText(gq);

        initView();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyGQAdapter(mBeanList);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.newGQ(GQActivity.this, pageNo + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshList();
            }
        });
    }

    private void refreshList() {
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.newGQ(GQActivity.this, pageNo + "");
    }

    @Override
    public void getInfoSuccess(MyBounsBean bean) {

    }

    @Override
    public void getInfoRecode(MyBounsRecodeBean bean) {

    }

    @Override
    public void getVipUpShSuccess(FriendBean bean) {

    }

    @Override
    public void getVipUpDataInfoSuccess(PronotionBean bean) {

    }

    @Override
    public void newBonusSuccess(NewBonusBean bean) {

    }

    @Override
    public void newBonusDetailSuccess(NewBonusDetailBean bean) {

    }

    @Override
    public void newBonusDonateSuccess(NewBonusDonateBean bean) {

    }

    @Override
    public void newGQSuccess(NewGQBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.page == null || bean.page.pageItems == null || bean.page.pageItems.size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        if (bean.page == null || bean.page.pageItems == null) {
            return;
        }

        mBeanList.addAll(bean.page.pageItems);
        mAdapter.notifyDataSetChanged();
        if (bean.page.pageItems.size() == 0 || bean.page.pageSize * bean.page.pageNumber >= bean.page.pageTotle) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }
}